<?php include "session.inc.php"; ?>
<?php require_once("../common/config.inc.php");?>
<?php include("../common/db.inc.php"); ?>
<?php
global $currentJournal,$divDisplayStyle,$operateResult,$currentDate;
	init();
	function init(){
		$GLOBALS[divDisplayStyle]=array();
		$operation=$_POST["operation"];
		//handle post request - modify user 
		if($operation && ($operation=="ModifyJournal")){
			$GLOBALS[divDisplayStyle]["operateDiv"]="display:none";
			$GLOBALS[divDisplayStyle]["operaterResultDiv"]="display:inline";
			updateJournal();
			$GLOBALS[operateResult]="Update journal succeed!";
		}else{
			$GLOBALS[divDisplayStyle]["operateDiv"]="display:inline";
			$GLOBALS[divDisplayStyle]["operaterResultDiv"]="display:none";		
			//handle get request - link to modify user page
			$id=$_GET["id"];				
			if($id && $id!="")	{				
				//echo "Journal ID is :".$id."<br/>";
				getJournal($id);			
			}else{
			     header("Location: JournalList.php");
			     exit;			
			}
		}
	}
	
	function getJournal($id){
		$sql = "SELECT * FROM medcdb.t_journal where id='$id'";
		$connection=getMEDCConnection();
		$userArray=executeQuery($sql,$connection);
		$GLOBALS[currentJournal]=$userArray[0];
		//echo "user id is ".$userid;
		//echo "<br/>user number is ".count($userArray);
		//echo "<br/> columns num is ".$GLOBALS[currentUser]['userid'];
	}	
	
	function updateJournal(){
		//$columnNames=array("id","name","facepic","storename","originalname","createdate","brief");	
		$id=$_POST["id"];
		$name=$_POST["name"];
		$facepic=$_POST["facepic"];
		$storename=$_POST["storename"];
		$originalname=$_POST["originalname"];
		$createdate=$_POST["createdate"];
		$brief=$_POST["brief"];				
		$sql=null;
		
		$sql = "update medcdb.t_journal set name='$name',facepic='$facepic',storename='$storename'," .
				"originalname='$originalname',originalname='$originalname',createdate='$createdate',brief='$brief'" .
				"where id='$id'";
		
		$connection=getMEDCConnection();
		executeUpdate($sql,$connection);
	}		
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="keywords" content="" />
<meta name="Description" content="" />
<meta name="author" content="" />
<title>Journal Modify Page</title>
<link rel="stylesheet" type="text/css" href="../css/medc.css" />
<script language="JavaScript" type="text/javaScript" src="../js/medc_common.js"></script>
<script language="JavaScript" type="text/javaScript" src="../js/CalendarPopup.js"></script>
<script language="JavaScript" type="text/javascript">	
	var CreateDateCalendar=null;
	
	function init(){
	    //set the scheduleTabCalendar
	    CreateDateCalendar = new CalendarPopup("CreateDateCalendarDiv");
	    CreateDateCalendar.setCssPrefix("TEST");
	    CreateDateCalendar.setReturnFunction("calendarReturnFunction");
	}
		
	function resetPass(){
	    var isConfirm = window.confirm("Are you sure to reset the user's password?");
	    if (isConfirm == false) {
	        return;
	    }	
		var newPass=getRandomCode(8);
		document.getElementById("password").readOnly=false;
		document.getElementById("password").value=newPass;
	}
	
	function calendarReturnFunction(y, m, d) {
	    if (window.CP_targetInput != null) {
	        var dt = new Date(y, m - 1, d, 0, 0, 0);
	        if (window.CP_calendarObject != null) {
	            window.CP_calendarObject.copyMonthNamesToWindow();
	        }
	        window.CP_targetInput.value = formatDate(dt, window.CP_dateFormat);
	    } else {
	        alert('Use setReturnFunction() to define which function will get the clicked results!');
	    }
	}
		
	function clickCalendar(calendarName){
		var inputName=null;
		var anchorName=null;
		var calendarObj=null;
		if(calendarName=="CreateDateCalendar"){
			inputName="createdate";
			anchorName="CreateDateCalendarImg";
			calendarObj=CreateDateCalendar;
			
		}
		var inputObj=document.getElementById(inputName);
		calendarObj.select(inputObj,anchorName,'yyyy-MM-dd HH:mm:ss');	
		return false;	
	}	
	
	function doPost(operation){
		if(operation=="saveJournal"){
		    var isConfirm = window.confirm("Are you sure to save changes?");
		    if (isConfirm == false) {
		        return;
		    }				
		}
		if(validate()==false){
			return;
		}
		document.getElementById("operation").value="ModifyJournal";
		//document.forms["medcform"].attributes["action"]="JournalModify.php";
		document.forms["medcform"].submit();
	}
	
	function validate(){
		
		//check Journal id,name,facepic,storename,originalname
		var journalId=document.getElementById("id").value;
		var journalName=document.getElementById("name").value;
		var facepic=document.getElementById("facepic").value;
		var storename=document.getElementById("storename").value;
		var originalname=document.getElementById("originalname").value;
		var createdate=document.getElementById("createdate").value;
		var checkList=new Array(
		["Journal Id",journalId],
		["Journal Name",journalName],
		["Cover Image",facepic],
		["Journal Store Name",storename],
		["Journal Original Name",originalname],
		["Journal Create Date",createdate]
		);
		for(var i=0;i<checkList.length;i++){
			if(isWhitespace(checkList[i][1])){
				alert(checkList[i][0]+ " can not be empty!");		
				return false;	
			}
		}
		return true;
	}
</script>
</head>
<body onload="init()">
	<div id="toparea">
		<?php include "../common/header.inc.php"?>
	</div>
	<div id="menu">
		<?php include "AdminMenu.inc.php";?>
	</div>	
	<!--Main area-->
	<div id="mainarea">
		<form method="post" action="JournalModify.php" name="medcform">
			<input type="hidden" name="operation" id="operation" />
			<div id="operateDiv" style="<? echo $GLOBALS[divDisplayStyle]["operateDiv"] ?>">
				<table>
					<tr>
						<td align="left"><label for="id">Journal Id</label></td>
						<td align="left"><input type="text" name="id" id="id" maxLength="30" size="20" readonly="true" value="<? echo $GLOBALS[currentJournal]['id']?>"/><td>
					</tr>
					<tr>
						<td align="left" style="width:120px;"><label for="name">Journal Name</label></td>
						<td align="left"><input type="text" name="name" id="name" maxLength="30" size="20" value="<? echo $GLOBALS[currentJournal]['name']?>"/><td>
					</tr>
					<tr>
						<td align="left"><label for="facepic">Cover Image</label></td>
						<td align="left"><input type="text" name="facepic" id="facepic" maxLength="100" size="40" value="<? echo $GLOBALS[currentJournal]['facepic']?>"/><td>
					</tr>	
					<tr>
						<td align="left"><label for="storename">Store Name</label></td>
						<td align="left"><input type="text" name="storename" id="storename" maxLength="100" size="40" value="<? echo $GLOBALS[currentJournal]['storename']?>"/><td>
					</tr>	
					<tr>
						<td align="left"><label for="originalname">Origninal Name</label></td>
						<td align="left"><input type="text" name="originalname" id="originalname" maxLength="30" size="20" value="<? echo $GLOBALS[currentJournal]['originalname']?>"/><td>
					</tr>			
					<tr>
						<td align="left"><label for="createdate">Create Date</label></td>
						<td nowrap="true" align="left" >		
							<div id="CreateDateDiv"><input name="createdate" id="createdate" type="text" title="Create Date" value="<? echo $GLOBALS[currentJournal]['createdate']?>"/>  
								<div id="CreateDateCalendarDiv" style="position:absolute;visibility:hidden;background-color:white;layer-background-color:white;"></div>
								<img src="../images/icon-select-date.gif" alt="select date icon" name="CreateDateCalendarImg" id="CreateDateCalendarImg" height="14" width="13"
								onclick="clickCalendar('CreateDateCalendar')" />
							</div>		
						</td>
					</tr>		
					<tr>
						<td align="left" valign="top" ><label for="brief">Brief</label></td>
						<td align="left"><textarea id="brief" name="brief" rows="3" ><? echo $GLOBALS[currentJournal]['brief']?></textarea><td>
					</tr>
					<tr>
						<td>&nbsp;</td>
						<td>
							<div align="right">
								<input type="button" name="ModifyJournal" value="Save" id="ModifyJournal" onclick="doPost('saveJournal')" />
							</div>	
						</td>
					</tr>
				</table>	
			</div>
			<!--end of modify user div -->
			<div id="operateResultDiv" style="<? echo $GLOBALS[divDisplayStyle]["operateResultDiv"] ?>" >
				<div align="center">
					<p> <?php echo $GLOBALS[operateResult]; ?></p> 
				</div>
			</div>
		</form>
	</div>
	
	<div class="myclear"></div>
	<div id="footer">
		<?php include "../common/footer.inc.php";?>
	</div>
</body>
</html>
